home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / musicali / splib52d.lha / superplay-lib_DEV / Programmers / Example_Tools / SimplePlay / SimplePlay.c < prev    next >
C/C++ Source or Header  |  1996-11-15  |  11KB  |  383 lines

  1. /* ======================================================================== */
  2. /* = Programmname    : SimplePlay V5.1                      = */
  3. /* =                                      = */
  4. /* ======================================================================== */
  5. /* = Author/Copyright : (c) 1994-96 by Andreas Ralph Kleinert.            = */
  6. /* =               Freeware. All rights reserved.              = */
  7. /* =                                      = */
  8. /* =                   Use it as an example for programming               = */
  9. /* =                   superplay.library !                                = */
  10. /* =                                      = */
  11. /* ======================================================================== */
  12. /* = Function          : SPObject operations :                             = */
  13. /* =                - Play Samples/Modules (all SPObjects)            = */
  14. /* ======================================================================== */
  15. /* = Last Update      : 8.8.1996                      = */
  16. /* =                                      = */
  17. /* ======================================================================== */
  18. /* = Remarks          : Needs "asl.library" V37+                          = */
  19. /* =                and "superplay.library" V5+.                      = */
  20. /* =                                      = */
  21. /* ======================================================================== */
  22. /* = Compiler          : SAS/C V6.56                       = */
  23. /* =                (smakefile)                                       = */
  24. /* ======================================================================== */
  25.  
  26. #include <intuition/intuitionbase.h>
  27.  
  28. #include <superplay/superplay.h>
  29.  
  30. #include <libraries/asl.h>
  31. #include <workbench/startup.h>
  32. #include <exec/interrupts.h>
  33.  
  34. #include <proto/exec.h>
  35. #include <proto/dos.h>
  36. #include <proto/intuition.h>
  37. #include <proto/graphics.h>
  38. #include <proto/asl.h>
  39. #include <proto/superplay.h>
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44.  
  45.  
  46.    /* Help- and Info- Texts */
  47.  
  48. char entry1_text  [] = "\2331;32;40mSimplePlay V5.1 \2330;32;40m\2333;32;40m(FREEWARE)\2330;32;40m\n(c) 1994-96 by Andreas Ralph Kleinert.\nAndreas R. Kleinert, Sandstrasse 1, D-57072 Siegen, Germany.\n";
  49. char entry2_text  [] = "Plays Sound Samples and Modules via superplay.library.\n";
  50. char entry3_text  [] = "USAGE : \2330;33;40mSimplePlay\2330;31;40m [? | -STOP | -REMOVE | <Sample/ModuleFileName>]\n";
  51.  
  52.  
  53. char ver_text [] = "\0$VER: SimplePlay 5.1 (8.8.96)";
  54.  
  55.  
  56. /* *************************************************** */
  57. /* *                             * */
  58. /* * Error-Messages for Leave() and KF_Message()     * */
  59. /* *                             * */
  60. /* *************************************************** */
  61.  
  62. char intuitionlib_text    [] = "You need \42intuition.library\42 V37+ !";
  63. char asllib_text     [] = "You need \42asl.library\42 V37+ !";
  64. char splib_text     [] = "You need \42superplay.library\42 V5+ !";
  65.  
  66.  
  67. /* *************************************************** */
  68. /* *                             * */
  69. /* * MACROs for Version-Tests                 * */
  70. /* *                             * */
  71. /* *************************************************** */
  72.  
  73. #define LibVer(x) ( ((struct Library *) x)->lib_Version )
  74. #define OS_VER      LibVer(SysBase)
  75.  
  76.  
  77. /* *************************************************** */
  78. /* *                             * */
  79. /* * Function Declarations                 * */
  80. /* *                             * */
  81. /* *************************************************** */
  82.  
  83. void __regargs SP_Play(char *filename);
  84. void __regargs SP_Remove(void);
  85.  
  86. void __regargs Leave(char *endtext, long code);
  87.  
  88.  
  89.    /* Functions from module "SimplePlay_Subs.o" : */
  90.  
  91. extern void __stdargs SP_Printf(char *formatstring, ...);
  92.  
  93.  
  94. /* *************************************************** */
  95. /* *                             * */
  96. /* * Additional Base Declarations             * */
  97. /* *                             * */
  98. /* *************************************************** */
  99.  
  100. extern struct ExecBase *SysBase;
  101.  
  102. struct IntuitionBase *IntuitionBase = N;
  103. struct SuperPlayBase *SuperPlayBase = N;
  104. struct Library       *AslBase       = N;
  105.  
  106.  
  107. /* *************************************************** */
  108. /* *                             * */
  109. /* * Variables for Port Usage                        * */
  110. /* *                             * */
  111. /* *************************************************** */
  112.  
  113. char portname   [] = "SimplePlay.port";
  114. char portname_2 [] = "SimplePlay_2.port";
  115.  
  116. struct MsgPort *port_1 = N;
  117. struct MsgPort *port_2 = N;
  118.  
  119.  
  120. /* *************************************************** */
  121. /* *                             * */
  122. /* * Structure-Definitions and Flags                 * */
  123. /* *                             * */
  124. /* *************************************************** */
  125.  
  126. struct SPMessage
  127. {
  128.  struct Message sp_Msg;  /* The Message itself           */
  129.  ULONG          sp_Flag; /* (See flag-definitions below) */
  130.  APTR           sp_Ext;  /* Future expansions            */
  131. };
  132.  
  133. #define SPF_REMOVE ((ULONG) 1)
  134. #define SPF_STOP   ((ULONG) 2)
  135. /* #define SPF_CONT   ((ULONG) 3) */
  136.  
  137.  
  138. /* *************************************************** */
  139. /* *                             * */
  140. /* * Globally Accessed Data                 * */
  141. /* *                             * */
  142. /* *************************************************** */
  143.  
  144. struct TextAttr __aligned chip topazattr = { (STRPTR) "topaz.font", TOPAZ_EIGHTY, FS_NORMAL, FPF_ROMFONT };
  145.  
  146. struct SPMessage __aligned our_msg =
  147. {
  148.  { N, N, N, NT_MESSAGE, 0, N },
  149.  N,
  150.  N
  151. };
  152.  
  153.  
  154. /* *************************************************** */
  155. /* *                             * */
  156. /* * MAIN                         * */
  157. /* *                             * */
  158. /* *************************************************** */
  159.  
  160. extern struct WBStartup *WBenchMsg;
  161. char                *wbloadname = N;
  162. long                     wb         = FALSE;
  163.  
  164. long  __stack        = 4096;
  165. char *__procname     = "SimplePlay V5.1";
  166. long  __priority     = 1;
  167. long  __BackGroundIO = TRUE;
  168.  
  169. extern BPTR _Backstdout;
  170.  
  171. void main(long argc, char **argv)
  172. {
  173.  port_1 = FindPort(portname);
  174.  
  175.  if(argc==0)
  176.   {
  177.    wb = TRUE;
  178.  
  179.    if(WBenchMsg->sm_NumArgs>1) wbloadname = WBenchMsg->sm_ArgList[1].wa_Name;
  180.   }
  181.  
  182.  if( argc > 2 || (argv[1][0] =='?') )
  183.   {
  184.    SP_Printf("%s%s%s", entry1_text, entry2_text, entry3_text);
  185.  
  186.    Leave(N, 0);
  187.   }
  188.  
  189.  IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37);
  190.  if(!IntuitionBase) Leave(intuitionlib_text, 100);
  191.  
  192.  AslBase = (struct Library *) OpenLibrary("asl.library", 37);
  193.  if(!AslBase) Leave(asllib_text, 102);
  194.  
  195.  SuperPlayBase = (struct SuperPlayBase *) OpenLibrary("superplay.library", 5);
  196.  if(!SuperPlayBase) Leave(splib_text, 102);
  197.  
  198.  
  199.  /* Play */
  200.  
  201.  if(argc < 2 && !wbloadname)
  202.   {
  203.    struct FileRequester     *request;
  204.    struct TagItem __aligned  tags[4];
  205.    char                      namebuffer [256];
  206.  
  207.    namebuffer[0] = (char) 0;
  208.  
  209.    tags[0].ti_Tag  = (Tag)   ASL_Hail;
  210.    tags[0].ti_Data = (ULONG) "Select Sample/Module to play :";
  211.  
  212.    tags[1].ti_Tag  = (Tag)   ASL_OKText;
  213.    tags[1].ti_Data = (ULONG) " Play ";
  214.  
  215.    tags[2].ti_Tag  = (Tag)   ASL_CancelText;
  216.    tags[2].ti_Data = (ULONG) " Quit ";
  217.  
  218.    tags[3].ti_Tag  = (Tag)   TAG_DONE;
  219.    tags[3].ti_Data = (ULONG) N;
  220.  
  221.    request = AllocAslRequest(ASL_FileRequest, N);
  222.    if(request)
  223.     {
  224.      if(AslRequest(request, &tags[0]))
  225.       {
  226.        strcpy(namebuffer, request->rf_Dir);
  227.        if(    (namebuffer[strlen(namebuffer)-1] !=      ':')
  228.            && (namebuffer[strlen(namebuffer)-1] !=      '/')
  229.            && (namebuffer[0]                    != (char) 0) ) strcat(namebuffer, "/");
  230.  
  231.        strcat(namebuffer, request->rf_File);
  232.  
  233.        if(namebuffer[0] != (char) 0) SP_Play(namebuffer);
  234.       }
  235.  
  236.      FreeAslRequest(request);
  237.     }
  238.   }else
  239.   {
  240.    if(wbloadname) SP_Play(wbloadname);
  241.     else
  242.      {
  243.       if(argv[1][0]!=(char) '-') SP_Play(argv[1]);
  244.        else
  245.         {
  246.          if(!stricmp(argv[1], "-STOP"))
  247.           {
  248.            if(port_1)
  249.             {
  250.              port_2 = CreatePort(portname_2, 0);
  251.  
  252.              our_msg.sp_Msg.mn_ReplyPort = port_2;
  253.              our_msg.sp_Msg.mn_Length    = sizeof(struct SPMessage);
  254.  
  255.              our_msg.sp_Flag             = SPF_STOP;
  256.              our_msg.sp_Ext              = N;
  257.  
  258.              PutMsg(port_1, (struct Message *) &our_msg);
  259.  
  260.              WaitPort(port_2);
  261.              GetMsg(port_2);
  262.  
  263.              RemPort(port_2);
  264.             }
  265.           }
  266.  
  267.          if(!stricmp(argv[1], "-REMOVE")) SP_Remove();
  268.         }
  269.      }
  270.   }
  271.  
  272.  Leave(N, 0);
  273. }
  274.  
  275.  
  276. /* *************************************************** */
  277. /* *                             * */
  278. /* * LEAVE : Global Exit Function Replacement         * */
  279. /* *                             * */
  280. /* *************************************************** */
  281.  
  282. void __regargs Leave(char *endtext, long code)
  283. {
  284.  if(SuperPlayBase) CloseLibrary((APTR) SuperPlayBase);
  285.  if(AslBase)       CloseLibrary((APTR) AslBase);
  286.  if(IntuitionBase) CloseLibrary((APTR) IntuitionBase);
  287.  
  288.  if(endtext)       if(!wb) SP_Printf("%s\n", endtext);
  289.  
  290.  exit(code);
  291. }
  292.  
  293. /* *************************************************** */
  294. /* *                             * */
  295. /* * Play-Function                     * */
  296. /* *                             * */
  297. /* *************************************************** */
  298.  
  299. void __regargs SP_Play(char *filename)
  300. {
  301.  APTR handle;
  302.  ULONG retval = SPERR_NO_ERROR;
  303.  struct SPMessage *got_msg      = N;
  304.  struct MsgPort   *our_waitport = N;
  305.  ULONG run = TRUE, flag;
  306.  
  307.  SP_Remove();
  308.  
  309.  our_waitport = CreatePort(portname, 0);
  310.  if(our_waitport)
  311.   {
  312.    handle = SPL_AllocHandle(N);
  313.    if(handle)
  314.     {
  315.      if(!(retval = SPL_InitHandleAsDOS(handle, N)))
  316.       {
  317.        if(!(retval = SPL_SuperPlay(handle, filename)))
  318.         {
  319.          for(;run;)
  320.           {
  321.            WaitPort(our_waitport);
  322.  
  323.            got_msg = (APTR) GetMsg(our_waitport);
  324.            flag = got_msg->sp_Flag;
  325.  
  326.            switch(flag)
  327.             {
  328.              case SPF_REMOVE : { SPL_FreeHandle(handle); run = FALSE; break; }
  329.              case SPF_STOP   : { SPL_StopReplay(handle);              break; }
  330.             }
  331.  
  332.            ReplyMsg((APTR) got_msg);
  333.           }
  334.         }
  335.       }
  336.    
  337.     }else retval = SPERR_NO_HANDLE;
  338.  
  339.    while( got_msg = (APTR)GetMsg(our_waitport) ) ReplyMsg((APTR)got_msg);
  340.  
  341.    RemPort(our_waitport);
  342.   }
  343.  
  344.  if(retval) Leave(SPL_GetErrorString(retval), 0);
  345. }
  346.  
  347. /* *************************************************** */
  348. /* *                             * */
  349. /* * Remove-Function                             * */
  350. /* *                             * */
  351. /* *************************************************** */
  352.  
  353. void __regargs SP_Remove(void)
  354. {
  355.  struct MsgPort *stfr_port = N;
  356.  
  357.  port_1 = FindPort(portname); /* still there ? */
  358.  if(port_1) /* remove previous song and task, if existant */
  359.   {
  360.    stfr_port = CreatePort(portname_2, 0);
  361.    if(stfr_port)
  362.     {
  363.      our_msg.sp_Msg.mn_ReplyPort = stfr_port;
  364.      our_msg.sp_Msg.mn_Length    = sizeof(struct SPMessage);
  365.  
  366.      our_msg.sp_Flag             = SPF_REMOVE;
  367.      our_msg.sp_Ext              = N;
  368.  
  369.       
  370.      port_1 = FindPort(portname); /* still there ? */
  371.      if(port_1)
  372.       {
  373.        PutMsg(port_1, (struct Message *) &our_msg);
  374.        WaitPort(stfr_port);
  375.       }
  376.  
  377.      while( GetMsg(stfr_port) ) ;
  378.  
  379.      RemPort(stfr_port);
  380.     }
  381.   }
  382. }
  383.